import pandas as pd
from pyecharts.charts import *
from pyecharts import options as opts

csv = pd.read_csv('../csv/vilipixiv.csv')
csv['tag'] = csv['tag'].map(lambda x: str(x)[1:-1])
# 取出tag列
tag = csv.loc[:, ['tag', 'like_count']]
tag.to_csv("../csv/tag.csv")
csv1 = pd.read_csv('../csv/tag.csv')

# 去除开头结尾的[]
# csv1['tag'] = csv1['tag'].map(lambda x: str(x)[1:-1])
# 将tag中按'，'分列
csv_tag = csv1['tag'].str.split(',', expand=True)
# 将tag中列转为行
csv_tag = csv_tag.stack()
# 重置索引，并命名（并删除多于的索引）
csv_tag = csv_tag.reset_index(level=1, drop=True)
csv_tag.name = 'tag_name'
# print(csv_tag)
# 将数据合并到原来数据
csv_new = csv1.drop(['tag'], axis=1).join(csv_tag)
# print(csv_new)
# 放入新csv并对列进行命名
csv_new.to_csv("../csv/tag_name.csv")
# 统计每个标签数量
csv2 = pd.read_csv('../csv/tag_name.csv')
# csv2['tag_name'] = csv2['tag_name'].map(lambda x: str(x)[1:-1])
counted = csv2.groupby(['tag_name']).size()
# counted['tag_name'] = counted['tag_name'].map(lambda x: str(x)[1:-1])
counted.to_csv("../csv/tag_count.csv", header=['tag_number'])
# 将tag_name按列排序
df = pd.read_csv('../csv/tag_count.csv')
csv3 = df.sort_values(by="tag_number", ascending=False)
csv3.to_csv("../csv/tag_count_1.csv")


# print(csv2)
data = []

def make_bar_2():
    data = pd.read_csv("../csv/tag_count_1.csv")
    data = data.head(50)
    x_data = list(data['tag_name'])
    y_data = list(data['tag_number'])
    # print(x_data)
    # print(y_data)
    bar = Bar()

    bar.add_xaxis(
        x_data)
    bar.add_yaxis("热度", y_data)
    bar.set_global_opts(
        title_opts=opts.TitleOpts(title='标签热度前50'),
        xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-50)),
    )

    bar.render('../html/标签热度柱状图.html')
    return bar


def get_pie_data(df, number, data):
    tag_data = (f'{df.iloc[number]["tag_name"]}', df.iloc[number]["tag_number"])
    # print(author_data)
    data.append(tag_data)
    return tag_data


def make_wordcloud():
    df = pd.read_csv("../csv/tag_count_1.csv")
    df = df.head(50)
    for i in range(0, 50):
        get_pie_data(df, i, data)
    data1 = [(" '#原创'", 56), (" '#女孩子'", 41), ("'#オリジナル'", 31), (" '#Genshin Impact'", 26), (" '#碧蓝档案'", 23), (" '#女の子'", 23), ("'#女の子'", 18), (" '#东方'", 18), (" '#オリジナル'", 18), ("'#ブルーアーカイブ'", 16), ("'#原神'", 14), (" '#創作'", 14), (" '#东方Project'", 13), (" '#插画'", 13), (" '#ブルアカ'", 13), (" '#young girl'", 13), (" '#原神'", 12), (" '#Hololive'", 11), (" '#バーチャルYouTuber100users入り'", 11), (" '#少女'", 11), (" '#彩虹社'", 11), (" '#Virtual Youtuber 100+ bookmarks'", 11), (" '#イラスト'", 11), (" '#GenshinImpact'", 10), (" '#虚拟主播'", 10), ("'#東方'", 10), (" '#芙宁娜'", 9), (" '#初音未来'", 9), (" '#東方'", 8), ("'#ぼっち・ざ・ろっく!'", 8), (" '#フリーナ'", 8), (" '#ブルーアーカイブ'", 7), (" '#ホロライブ'", 7), (" '#BlueArchive'", 7), (" '#東方Project'", 7), (" '#赛马娘Pretty Derby'", 7), ("'#GenshinImpact'", 6), (" '#猫耳'", 6), (" '#cat ears'", 6), (" '#にじさんじ'", 6), ("'#創作'", 6), ("'#東方Project'", 6), (" '#世界计划'", 6), (" '#バーチャルYouTuber'", 6), (" '#背景'", 6), (" '#VTuber'", 6), (" '#芙宁娜（原神）'", 6), (" '#background'", 6), (" '#制服'", 5), (" '#风景'", 5)]
    wcd = WordCloud()
    wcd.add(series_name="热点分析", data_pair=data1, word_size_range=[20, 88])
    wcd.set_global_opts(
        title_opts=opts.TitleOpts(
            title="热点分析",
            title_textstyle_opts=opts.TextStyleOpts(font_size=23),
            pos_left="center"
        ),
        tooltip_opts=opts.TooltipOpts(is_show=True),
    )
    wcd.render('../html/热力分析云图.html')
    return wcd


if __name__ == "__main__":
    make_bar_2()
    make_wordcloud()
